import * as Cesium from 'cesium'
import { getAssetsFile } from "@/utils/utils";
import riverJson from '@/assets/data/river.json'

const waterSurface=(viewer:any,type:string)=>{
    removePri(viewer);
    let material=null;
    if(type==="color"){
        material = new Cesium.Material({
            fabric: {
                type: "Water",
                uniforms: {
                    baseWaterColor: new Cesium.Color(64 / 255.0, 157 / 255.0, 253 / 255.0, 0.5),
                    frequency: 8000.0, // 控制波数的数字。
                    animationSpeed: 0.02, // 控制水的动画速度的数字。
                    amplitude: 5.0, // 控制水波振幅的数字。
                    specularIntensity: 0.8, // 控制镜面反射强度的数字。
                }
            }
        })
    }else{
         material = new Cesium.Material({
            fabric: {
                type: "Water",
                uniforms: {
                    normalMap: getAssetsFile('/water/waterNormals.jpg'),
                    frequency: 8000.0, // 控制波数的数字。
                    animationSpeed: 0.02, // 控制水的动画速度的数字。
                    amplitude: 5.0, // 控制水波振幅的数字。
                    specularIntensity: 0.8, // 控制镜面反射强度的数字。
                }
            }
        })
    }
    let features= riverJson['features'];
    let instances:any=[];
    features.forEach((item:any)=>{
        let coordinates=item.geometry.coordinates.join(',');
       let newCoordinate= coordinates.split(',').map((item:any)=>Number(item))
        instances.push( new Cesium.GeometryInstance({
            geometry: new Cesium.PolygonGeometry({
                polygonHierarchy: new Cesium.PolygonHierarchy(
                    Cesium.Cartesian3.fromDegreesArray(newCoordinate)
                ),
                vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT,
                extrudedHeight:-20,
                height: 10
            })
        }));
    })

    let primitives = new Cesium.Primitive({
        geometryInstances:instances,
        appearance: new Cesium.EllipsoidSurfaceAppearance({
            material: material
        }),
        vertexCacheOptimize:true,
    })
    viewer.scene.primitives.add(
        primitives
    );
}
const removePri=(viewer:any)=>{
    viewer.scene.primitives.removeAll();
}
export {
    waterSurface
}
